package com.kly.smartbi.controller.bi;

import com.kly.smartbi.common.BaseResponse;
import com.kly.smartbi.common.ErrorCode;
import com.kly.smartbi.common.ResultUtils;
import com.kly.smartbi.exception.ThrowUtils;
import com.kly.smartbi.manager.FileManager;
import com.kly.smartbi.model.dto.post.UploadResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author admin
 * @date 2025/4/8
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);
    @Resource
    private FileManager fileManager;


    /**
     * 文件上传
     */
    @PostMapping("/upload/avatar")
    public BaseResponse<UploadResult> uploadAvatar(@RequestPart("file") MultipartFile file) {
        ThrowUtils.throwIf(file.isEmpty(), ErrorCode.PARAMS_ERROR, "上传文件为空");
        UploadResult uploadResult = fileManager.uploadFile(file, "avatar");

        return ResultUtils.success(uploadResult);
    }

    @PostMapping("/upload/post")
    public BaseResponse<UploadResult> uploadPicture(@RequestPart("file") MultipartFile file) {
        ThrowUtils.throwIf(file.isEmpty(), ErrorCode.PARAMS_ERROR, "上传文件为空");
        UploadResult uploadResult = fileManager.uploadFile(file, "post");
        return ResultUtils.success(uploadResult);
    }


}
